﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using MagicBag.CRM.Model;
using MagicBag.CRM.MiddleModel.EasyUI;
using MagicBag.CRM.BLL;

namespace MagicBag.CRM.Service
{
    public class MenuService : BaseService
    {
        public void Insert(HttpContext context)
        {
            Menu menu = new Menu();

            menu.Name = context.Request["Name"];
            int parentID;
            Int32.TryParse(context.Request["ParentID"], out parentID);
            menu.ParentID = parentID;
            menu.Remark = context.Request["Remark"];

            Result result = new Result();
            result.Success = true;

            //检测橙色是否已存在

            try
            {
                MenuBLL menuBLL = new MenuBLL();
                menuBLL.Insert(menu);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="context"></param>
        public void Delete(HttpContext context)
        {
            Result result = new Result();
            result.Success = true;

            int id;
            Int32.TryParse(context.Request["id"], out id);

            try
            {
                MenuBLL menuBLL = new MenuBLL();
                menuBLL.Delete(id);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }

        public void Update(HttpContext context)
        {
            Menu menu = new Menu();
            int id;
            Int32.TryParse(context.Request["ID"], out id);
            menu.ID = id;
            menu.Name = context.Request["Name"];
            menu.Location = context.Request["Location"];

            int pid;
            Int32.TryParse(context.Request["ParentID"], out pid);
            menu.ParentID = pid;

            menu.Remark = context.Request["Remark"];

            Result result = new Result();
            result.Success = true;
            try
            {
                MenuBLL menuBLL = new MenuBLL();
                menuBLL.Update(menu);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            ResponseJson(context, result.ToString());
        }

        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <param name="context"></param>
        public void GetMenus(HttpContext context)
        {
            MenuBLL menuBLL = new MenuBLL();
            IList<Menu> list = menuBLL.GetMenus();
            string s = string.Empty;

            //将查询结果转换成EasyUI结点对象列表
            IList<MenuTreeNode> menuTreeNodeList = (from m in list
                                                    select new MenuTreeNode
                                                    {
                                                        id = m.ID,
                                                        text = m.Name,
                                                        attributes = new MenuTreeAttributes { location = m.Location, remark = m.Remark }
                                                    }
                                                    ).ToList();

            if (context.Request["ddl"] == "1")//返回菜单信息，提供给下拉列表使用
            {
                MenuTreeNode defualtMenu = new MenuTreeNode(0, "无");
                menuTreeNodeList.Insert(0, defualtMenu);
                s = JsonHelper.GetJson<IList<MenuTreeNode>>(menuTreeNodeList);
            }
            else
            {


                IList<MenuTreeNode> menuToJson = new List<MenuTreeNode>();

                foreach (MenuTreeNode menu in menuTreeNodeList)//设置每一个菜单节点的直接子节点
                {
                    //从list中查询出当前节点的直接子节点ID
                    List<int> childrenIDs = (from m in list
                                             where m.ParentID == menu.id
                                             select m.ID).ToList();

                    IList<MenuTreeNode> childrens = (from m in menuTreeNodeList from id in childrenIDs where m.id == id select m).ToList();

                    if (childrens != null && childrens.Count > 0)
                        menu.children = childrens;//这一步仅仅是设置子节点而已
                    //查询根节点添加到json化列表
                    List<int> rootIDs = (from m in list
                                         where m.ParentID == 0
                                         select m.ID).ToList();
                    menuToJson = (from m in menuTreeNodeList from id in rootIDs where m.id == id select m).ToList();//根节点才是要json化的
                }
                s = JsonHelper.GetJson<IList<MenuTreeNode>>(menuToJson);
            }
            ResponseJson(context, s);
        }
    }
}
